package com.cwn.wethink.common.utils.rar;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by wangshunyao on 2017/5/5.
 */
public class CompressOutItemStructure {
    static int fileNumber = 0;
    static Item[] items = null;
    static String parent = "";
 
    /**
     * file item format is fileName String,btye[] content
     * folder dir+"/"+fileName,byte[] content
     * @param compressDir
     * @return
     */
    public static Item[] create(String compressDir) {
        File file = new File(compressDir);
        parent = file.getParent() == null ? "":file.getParent();
        fileCounter(file);
        items = new Item[fileNumber];
        packet(file);
        return items;
    }
 
    public static void fileCounter(File file){
        if (!file.exists()){
            throw new RuntimeException("not found file "+file.getPath());
        }
        if (file.isFile()){
            fileNumber++;
        }else{
            for(File inFile : file.listFiles()){
                if (inFile.isFile()){
                    fileNumber++;
                }else{
                    fileCounter(inFile);
                }
            }
        }
    }
    static int tempFileIndex = 0;
    public static void packet(File file){
        if (!file.exists()){
            throw new RuntimeException("not found file "+file.getPath());
        }
        if (file.isFile()){
            items[tempFileIndex] = readFile(file);
            tempFileIndex++;
        }else{
            for(File file1 : file.listFiles()){
                if (file1.isFile()){
                    items[tempFileIndex] = readFile(file1);
                    tempFileIndex++;
                }else{
                    packet(file1);
                }
            }
        }
    }
 
    public static Item readFile(File file) {
        String path = file.getPath();
        if (!("".equals(parent))){
            path = file.getPath().replace(parent,"");
        }
        return  new Item(path,fileToBytes(file));
    }
    public static byte[] fileToBytes(File file) {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        byte[] resultBytes = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while((len = fis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            resultBytes = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  resultBytes;
    }
 
}